package cn.springBoot;

import java.util.Random;

import org.springframework.http.MediaType;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;

/**
 * <p>
 * 生成短信验证码 原理 --> 收到电话号 - 生成随机数 - 发送消息 - 返回结果 - 存到rides 或者session 由前端进行匹配
 * </p>
 *
 * @author ljx
 * @class cn.springBoot.TTT
 * @create date: 2021/12/15
 */
public class TTT {
    public static void main(String[] args) {

        // 生成随机数
        String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
        System.out.println("verifyCode: " + verifyCode);

        // 地址
        String urls = "https://api-zj-mix2.zthysms.com/v2/sendSmsTp";
        // 请求入参
        JSONObject requestJson = new JSONObject();
        // 账号
        requestJson.put("username", "xxxx");
        // tKey
        long tKey = System.currentTimeMillis() / 1000;
        requestJson.put("tKey", tKey);
        // 明文密码
        requestJson.put("password", SecureUtil.md5(SecureUtil.md5("xxxx") + tKey));
        // 模板ID
        requestJson.put("tpId", 46035);
        // 签名
        requestJson.put("signature", "【北京冷新图文科技有限责任公司】");

        // 发送记录集合
        JSONArray records = new JSONArray();
        JSONObject record = new JSONObject();
        // 手机号
        record.put("mobile", "15711164905");
        // 替换变量
        JSONObject param = new JSONObject();
        param.put("valid_code", verifyCode);
        record.put("tpContent", param);
        records.add(record);
        requestJson.put("records", records);
        String result = HttpRequest.post(urls).timeout(60000)
            .body(requestJson.toJSONString(), MediaType.APPLICATION_JSON_UTF8_VALUE).execute().body();
        System.out.println(result);

        // {"code":200,"msg":"success","tpId":"46035","msgId":"163953849841589963521","invalidList":[]}

        JSONObject jsonObject = JSON.parseObject(result);
        String code = jsonObject.getString("code");

        if (code.equals(200)) {

        } else {

        }

    }
}
